<?php


namespace App\Http\Controllers\Property;

use App\Common\json;
use App\Http\Controllers\Controller;
//use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ReleaseController extends  Controller{



    //发布信息
    public function release(Request $request){
        if($request ->isMethod('post')){
            $array       = (object)array();
            $content      = $request->input('content');
            $user_release  = $request ->input('user_id');
            // $village_id    = $request ->input('village_id');
            $release_image  = $request->input('release_image');
            $position       = $request ->input('position');
            $release_operation = $request ->input('release_operation');
            $creation_time  = date('Y-m-d H:i:s');
            $type           = $request ->input('release_type');
            $data           = DB::table('release')
                ->insert(
                    ['user_release' =>$user_release,'content'=>$content,'release_image'=>$release_image,
                        'position'    =>$position,'release_create_time' => $creation_time,'release_operation'=>$release_operation
                        ,'release_type' =>$type
                    ]
                );
            if($data  == true){
                return json::json('success','获取发布信息成功',$array);
            }else{
                return json::json('failure','由于网络或者不正当原因注册失败',$array);
            }
        }
    }
    //获取自己发布的信息
    public function selectrelease(Request $request){
        if($request ->isMethod('post')){
            $array = [];
            $user_id       = $request ->input('user_id');
            $data                = DB::table('release')
                // ->select('release_id','content','release_image','position','phone','nickname','release_create_time','release_operation')
                ->leftjoin('user','user.user_id','=','release.user_release')
                ->where('user_release',$user_id)
                ->orderBy('release_id', 'DESC')
                ->get();
            foreach ($data as $value){
                if(empty($value->release_image)){
                    $release_image = [];
                }else {
                    $release_image = explode(",", $value->release_image);
                }
                $da[] = array(
                    'user_id' =>$value->user_id,
                    'release_id' => $value ->release_id,
                    'content'=> $value ->content,
                    'position' => $value ->position,
                    'phone' => $value ->phone,
                    'nickname'=> $value ->nickname,
                    'release_image'=>$release_image,
                    'date'=>$value ->release_create_time,
                    'release_operation'=>$value ->release_operation
                );
            }
            if($data  == true){
                return json::json('success','获取发布信息成功',$da);
            }else{
                return json::json('failure','由于网络或者不正当原因注册失败',$array);
            }
        }
    }

    //修改发布信息
    public function updaterelease(Request $request){
        if($request -> isMethod('post')){
            $arr                = (object)array();
            // $user_start       = $request ->input('user_start');
            $release_id          = $request ->input('release_id');
            $content      = $request->input('content');
            $release_image  = $request->input('release_image');
            $position       = $request ->input('position');
            $update_time         = date('Y-m-d H:i:s');
            $data             = DB::table('release')
                ->where('release_id',$release_id)
                ->update([
                    'content' =>$content,
                    'release_image' =>$release_image,
                    'position'      => $position,
                    'update_time'   => $update_time
                ]);
            if($data   == true){
                return json::json('success','数据修改成功',$arr);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }

        }
    }


    //删除发布信息
    public function delrelease(Request $request){
        if($request ->isMethod('post')){
            $arr                = (object)array();
            $release_id          = $request ->input('release_id');
            $data                = DB::table('release')
                ->where('release_id','=',$release_id)
                ->delete();
            if($data   == true){
                return json::json('success','删除发布信息成功',$arr);
            }else{
                return json::json('failure','由于网络或者不正当原因删除失败',$arr);
            }
        }

    }

    //点赞数
    public function like(Request $request){
        if($request ->isMethod('post')){
            $user_id       = $request ->input('user_id');
            $release_id   = $request ->input('release_id');
            $type         = $request ->input('type');
            $select        = DB::table('like')
                ->where('user_like','=',$user_id)
                ->where('release_like','=',$release_id)
                ->get();
            if($select == true){
                $data       = DB::table('release')
                    ->select('like')
                    ->where('release_id',$release_id)
                    ->first();
                $update   = DB::table('release')
                    ->where('release_id',$release_id)
                    ->update(['like'=>($data->like-1)]);
                if($update   == true){
                    DB::table('like') ->where('user_like','=',$user_id)
                        ->where('release_like','=',$release_id) ->delete();
                    return json::json('success','点击-1',json::value(1));
                }else{
                    return json::json('failure','由于网络或者不正当原因失败',json::value(1));
                }
            }else{
                $data       = DB::table('release')
                    ->select('like')
                    ->where('release_id',$release_id)
                    ->first();
                $update   = DB::table('release')
                    ->where('release_id',$release_id)
                    ->update(['like'=>($data->like+1)]);
                if($update   == true){
                    DB::table('like')->insert([
                        'user_like'           => $user_id,
                        'release_like'        => $release_id,
                        'like_type'           =>1
                    ]);
                    return json::json('success','点击+1',json::value(1));
                }else{
                    return json::json('failure','由于网络或者不正当原因失败',json::value(1));
                }
            }
        }
    }

    public function pro($user_id =null,$village_name,$type){
        if($type ==1) {
            $data   = DB::table('release')
                ->leftjoin('user','user.user_id','=','release.user_release')
                ->where('position',$village_name)
                ->where('release_type',1)
                ->orderBy('release_id', 'DESC')
                ->get();
            return $data;
        }elseif($type ==2){
            $data   = DB::table('release')
                ->leftjoin('work','work.work_id','=','release.user_release')
                ->where('position',$village_name)
                ->where('release_type',2)
                ->orderBy('release_id', 'DESC')
                ->get();
            return $data;
        }elseif($type ==3){
            $data   = DB::table('release')
                ->leftjoin('user','user.user_id','=','release.user_release')
                ->where('position','!=',$village_name)
                ->where('release_type',1)
                ->orderBy('release_id', 'DESC')
                ->get();
            return $data;
        }elseif($type ==4){
            $data   = DB::table('release')
                ->leftjoin('work','work.work_id','=','release.user_release')
                ->where('position','!=',$village_name)
                ->where('release_type',2)
                ->orderBy('release_id', 'DESC')
                ->get();
            return $data;
        }
    }



    public function AllRelease(Request $request){
        if($request ->isMethod('post')){
            $user_id        = $request ->input('user_id');
            $village_name   = $request ->input('village_name');
            $dataone        = $this ->pro($user_id,$village_name,1);
            $datatwo        = $this ->pro($user_id,$village_name,2);
            // return $datatwo;
            $data           = array_merge($dataone,$datatwo);
            //return $data;
            foreach ($data as $key =>$value){
                if(empty($value ->release_image)){
                    $release_image  =[];
                }else {
                    $release_image = explode(',', $value->release_image);
                }
                if($value ->release_type ==1){
                    $head          = $value ->head;
                    $nickname      = $value ->nickname;
                    $user_ids      = $value->user_id;
                }else{
                    $head          = $value ->work_head;
                    $nickname      = $value ->work_nickname;
                    $user_ids       = $value  ->work_id;
                }
                $id            = $value ->release_id;
                /*                $city          = $value ->city;
                                $district      = $value ->district;*/
                $content       = $value ->content;
                $release_type  = $value ->release_type;
                $like          = $value ->like;
                $position      = $value ->position;
                $time = $value ->release_create_time;
                $release_operation = $value ->release_operation;
                $Nearby        = $this->Nearby($id);
                $estimate      = $this ->estimate($user_id,$id);
                if($estimate == null){
                    $ex   = '0';
                }else{
                    $ex = '1';
                }

                $da[] = array(
                    'user_id' =>$user_ids,
                    'release_id'=>$id,'release_image' => $release_image, 'head' => $head, 'nickname' => $nickname,
                    'content' => $content, 'like' => $like, 'release_create_time' => $time,'release_type'=>$release_type,
                    'position' => $position, 'sum' =>$Nearby,'estimate'=>$ex,'release_operation'=> $release_operation
                );
            }
            foreach ($da as $k =>$v){
                $times[$k]  =$v['release_create_time'];
                $release_id[$k] = $v['release_id'];
            }
            array_multisort($release_id, SORT_DESC, $times, SORT_ASC, $da);
            if($data == true){
                return json::json('success', '获取全部数据成功', $da);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(2));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }
    //统计评论数
    public function Nearby($id){
        $data        = DB::table('release_topic')
            ->where('release_releasetopic',$id)
            ->count('release_releasetopic');
        return $data;
    }

    //判断是否点击过
    public function estimate($id,$release_id){
        $data        = DB::table('like')
            ->where('user_like',$id)
            ->where('release_like',$release_id)
            ->get();
        return $data;
    }


    public function NearbyRelease(Request $request){
        if($request ->isMethod('post')){
            $user_id        = $request ->input('user_id');
            $village_name   = $request ->input('village_name');
            $dataone        = $this ->pro($user_id,$village_name,3);
            $datatwo        = $this ->pro($user_id,$village_name,4);
            $data           = array_merge($dataone,$datatwo);
            foreach ($data as $key =>$value){
                if(empty($value ->release_image)){
                    $release_image  =[];
                }else {
                    $release_image = explode(',', $value->release_image);
                }
                if($value ->release_type ==1){
                    $head          = $value ->head;
                    $nickname      = $value ->nickname;
                    $user_ids       = $value->user_id;
                }else{
                    $head          = $value ->work_head;
                    $nickname      = $value ->work_nickname;
                    $user_ids       = $value  ->work_id;
                }
                $id            = $value ->release_id;
                /*                $city          = $value ->city;
                                $district      = $value ->district;*/
                $content       = $value ->content;
                $like          = $value ->like;
                $position      = $value ->position;
                $release_type  = $value ->release_type;
                $time = $value ->release_create_time;
                $release_operation = $value ->release_operation;
                $Nearby        = $this->Nearby($id);
                $estimate      = $this ->estimate($user_id,$id);
                if($estimate == null){
                    $ex   = '0';
                }else{
                    $ex = '1';
                }

                $da[] = array(
                    'user_id' =>$user_ids,
                    'release_id'=>$id,'release_image' => $release_image, 'head' => $head, 'nickname' => $nickname,
                    'content' => $content, 'like' => $like, 'release_create_time' => $time,'release_type'=>$release_type,
                    'position' => $position, 'sum' =>$Nearby,'estimate'=>$ex,'release_operation'=> $release_operation
                );
            }
            foreach ($da as $k =>$v){
                $times[$k]  =$v['release_create_time'];
                $release_id[$k] = $v['release_id'];
            }
            array_multisort($release_id, SORT_DESC, $times, SORT_ASC, $da);
            if($data == true){
                return json::json('success', '获取全部数据成功', $da);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(2));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //论坛文章
    public  function primary($release_id){
        $data = DB::table('release')
            ->leftjoin('user','release.user_release','=','user.user_id')
            ->where('release_id',$release_id)
            ->get();
        return $data;
    }

    //跟贴内容
    public  function secondary($release_id){
        $data = DB::table('release_topic')
            ->leftjoin('user','release_topic.user_releasetopic','=','user.user_id')
            ->where('release_releasetopic',$release_id)
            ->orderby('releasetopic_id','ASE')
            ->get();
        return $data;
    }


    //获取谁点评过
    public function Releaselike($release_id){
        $data = DB::table('like')
            ->leftjoin('user','like.user_like','=','user.user_id')
            ->where('release_like',$release_id)
            ->where('like_type',1)
            ->get();
        return $data;
    }
    public function Villagelike($release_id){
        $data = DB::table('like')
            ->leftjoin('village','like.user_like','=','village.village_id')
            ->where('release_like',$release_id)
            ->where('like_type',2)
            ->get();
        return $data;
    }



    public  function secondar($release_id){
        $data = DB::table('release_topic')
            ->select('head','nickname','releasetopic_content','userID','releasetopic_id','release_releasetopic','user_releasetopic','release_operation')
            ->leftjoin('user','release_topic.user_releasetopic','=','user.user_id')
            ->leftjoin('release','release_topic.release_releasetopic','=','release.release_id')
            ->where('release_releasetopic',$release_id)
            ->orderby('releasetopic_id','ASE')
            ->get();

        return $data;
    }
   /* public function village_secondar($release_id){
        $data = DB::table('release_topic')
            ->select('work_head','work_nickname','releasetopic_content','userID','releasetopic_id','release_releasetopic','user_releasetopic','release_operation')
            ->leftjoin('work','release_topic.user_releasetopic','=','work.work_id')
            ->leftjoin('release','release_topic.release_releasetopic','=','release.release_id')
            ->where('release_releasetopic',$release_id)
            ->where('operate_type',2)
            ->orderby('releasetopic_id','ASE')
            ->get();

        return $data;
    }*/
    public  function secos($release_id){
        $data = DB::table('release_topic')
            ->select('head','nickname','releasetopic_content','userID','releasetopic_id','release_releasetopic','user_releasetopic')
            ->leftjoin('user','release_topic.userID','=','user.user_id')
            ->where('release_releasetopic',$release_id)
            ->orderby('releasetopic_id','ASE')
            ->get();
        return $data;

    }
    public function Particulars($name,$result=array())
    {
        global $result;
        $res    = '';
        $data = $this->secondar($name);
        foreach ($data as $key => $val) {
            for ($i=0; $i<count($result);$i++) {
                $id =  $result[$i]['releasetopic_id'];
                $d  = $this ->secos($id);
                foreach ($d as $v){
                    $res = $v ->nickname;
                }
            }
            $release_operation   = $val ->release_operation;
            if($release_operation ==1){
                $head   = "http://www.mukitech.com/wuye_backend/upload/image/rotation_img310447598.jpg";
                $nickname     ="匿名";
                $result[] = array(
                    'head' => $head,
                    'nickname' => $nickname,
                    'releasetopic_content' => $val->releasetopic_content,
                    'release_releasetopic' => $val->release_releasetopic,
                    'userID' => $val->userID,
                    'nickname1' =>$res,
                    'user_releasetopic' =>$val ->user_releasetopic,
                    'releasetopic_id' => $val->releasetopic_id,
                );
            }else{
                $head   = $val->head;
                $nickname     =$val->nickname;
                $result[] = array(
                    'head' => $head,
                    'nickname' => $nickname,
                    'releasetopic_content' => $val->releasetopic_content,
                    'release_releasetopic' => $val->release_releasetopic,
                    'userID' => $val->userID,
                    'nickname1' =>$res,
                    'user_releasetopic' =>$val ->user_releasetopic,
                    'releasetopic_id' => $val->releasetopic_id,
                );
            }


            $this->Particulars($val->releasetopic_id);

        }
        return $result;
    }
    public function Village_Particulars($name,$results=array()){
        global $results;
        $res    = '';
        $data = $this ->village_secondar($name);
        foreach ($data as $key => $val) {
            for ($i=0; $i<count($results);$i++) {
                $id =  $results[$i]['releasetopic_id'];
                $d  = $this ->secos($id);
                foreach ($d as $v){
                    $res = $v ->nickname;
                }
            }
            $release_operation   = $val ->release_operation;
            if($release_operation ==1){
                $head   = "http://www.mukitech.com/wuye_backend/upload/image/rotation_img310447598.jpg";
                $nickname     ="匿名";
                $results[] = array(
                    'head' => $head,
                    'nickname' => $nickname,
                    'releasetopic_content' => $val->releasetopic_content,
                    'release_releasetopic' => $val->release_releasetopic,
                    'userID' => $val->userID,
                    'nickname1' =>$res,
                    'user_releasetopic' =>$val ->user_releasetopic,
                    'releasetopic_id' => $val->releasetopic_id,
                );
            }else{
                $head   = $val->work_head;
                $nickname     =$val->work_nickname;
                $results[] = array(
                    'head' => $head,
                    'nickname' => $nickname,
                    'releasetopic_content' => $val->releasetopic_content,
                    'release_releasetopic' => $val->release_releasetopic,
                    'userID' => $val->userID,
                    'nickname1' =>$res,
                    'user_releasetopic' =>$val ->user_releasetopic,
                    'releasetopic_id' => $val->releasetopic_id,
                );
            }


            $this->Village_Particulars($val->releasetopic_id);

        }
        return $results;
    }


    public  function primarytw($release_id){
            $data = DB::table('release')
                ->leftjoin('user', 'release.user_release', '=', 'user.user_id')
                ->where('release_id', $release_id)
                ->get();
            foreach ($data as $k) {
                $id = $k->release_id;
                $release_operation = $k->release_operation;
                if ($release_operation == 1) {
                    $headp = "http://www.mukitech.com/wuye_backend/upload/image/rotation_img310447598.jpg";
                    $nickname = "匿名";
                    $address = $k->position;
                    $content = $k->content;

                    $like = $k->like;
                    if (empty($k->release_image)) {
                        $image = [];
                    } else {
                        $image = explode(',', $k->release_image);
                    }
                    $release_create_time = $k->release_create_time;
                    $pro = $this->Particulars($id);


                   if (!empty($pro)) {
                        $ddpss = $pro;
                    } else {
                        $ddpss = [];
                    }
                    if ($ddpss == true) {
                        $db = $ddpss;
                    } else {
                        $db = [];
                    }

                    $likes = $this->Releaselike($id);
                   /* $villagelike = $this->Villagelike($id);

                    foreach ($villagelike as $liks) {
                        $heads = "http://www.mukitech.com/wuye_backend/upload/image/rotation_img310447598.jpg";
                        $user_id = $liks->user_like;
                        $village[] = array('head' => $heads, 'user_id' => $user_id);
                    }*/

                    foreach ($likes as $lik) {
                        $heads = "http://www.mukitech.com/wuye_backend/upload/image/rotation_img310447598.jpg";
                        $user_id = $lik->user_like;
                        $likeb[] = array('head' => $heads, 'user_id' => $user_id);
                    }

                   /* if (!empty($village) && !empty($likeb)) {
                        $lk = array_merge($village, $likeb);
                    } elseif (!empty($village)) {
                        $lk = $village;
                    } else*/if (!empty($likeb)) {
                        $lk = $likeb;
                    } else {
                        $lk = [];
                    }
                    $da = array(
                        'user_id' => $k->user_id,
                        'head' => $headp,
                        'release_operation' => $k->release_operation,
                        'release_create_time' => $release_create_time, 'nickname' => $nickname, 'address' => $address, 'content' => $content, 'release_image' => $image,
                        'like' => $like, 'list' => $db, 'lik' => $lk
                    );

                } else {
                    $headp = $k->head;
                    $nickname = $k->nickname;
                    $address = $k->position;
                    $content = $k->content;
                    $like = $k->like;
                    if (empty($k->release_image)) {
                        $image = [];
                    } else {
                        $image = explode(',', $k->release_image);
                    }
                    $release_create_time = $k->release_create_time;
                    $pro = $this->Particulars($id);

                    if (!empty($pro)) {
                        $ddpss = $pro;
                    } else {
                        $ddpss = [];
                    }
                    if ($ddpss == true) {
                        $db = $ddpss;
                    } else {
                        $db = [];
                    }

                    $likes = $this->Releaselike($id);
                   /* $villagelike = $this->Villagelike($id);

                    foreach ($villagelike as $liks) {
                        $heads = $liks->work_head;
                        $user_id = $liks->user_like;
                        $village[] = array('head' => $heads, 'user_id' => $user_id);
                    }*/

                    foreach ($likes as $lik) {
                        $heads = $lik->head;
                        $user_id = $lik->user_like;
                        $likeb[] = array('head' => $heads, 'user_id' => $user_id);
                    }
/*
                    if (!empty($village) && !empty($likeb)) {
                        $lk = array_merge($village, $likeb);
                    } elseif (!empty($village)) {
                        $lk = $village;
                    } else*/if (!empty($likeb)) {
                        $lk = $likeb;
                    } else {
                        $lk = [];
                    }

                    $da = array(
                        'user_id' => $k->user_id,
                        'head' => $headp,
                        'release_operation' => $k->release_operation,
                        'release_create_time' => $release_create_time, 'nickname' => $nickname, 'address' => $address, 'content' => $content, 'release_image' => $image,
                        'like' => $like, 'list' => $db, 'lik' => $lk
                    );
                }

            }
            return $da;



    }

    //获取详情
    public function ReleaseParticulars(Request $request){
        if($request -> isMethod('post')){
            $array     = [];
            $release_id  = $request ->input('release_id');
            $key         = $this->primarytw($release_id);

            if(empty($key)){
                $kd  = $array;
            }else{
                $kd  = $key;
            }


            if($key == true) {
                return json::json('success', '获取详情数据成功', $kd);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$array);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }


    public function InfoRelease(Request $request){
        if($request ->isMethod('get')) {
            //$like           = $this ->estimate($user_id);
            $data = DB::table('release')
                ->leftjoin('user', 'user.user_id', '=', 'release.user_release')
                ->leftjoin('village','user.user_village','=','village.village_id')
                ->orderby('like','DESC')
                ->paginate(20)->toArray();
            $da = array_values(array_splice($data, 8));
            foreach ($da as $key =>$value){
                foreach ($value as $v){
                    $user_id       = $v ->user_id;
                    $id            = $v ->release_id;
                    if(empty($value ->release_image)){
                        $release_image  = [];
                    }else {
                        $release_image = explode(',', $value->release_image);
                    }
                    $release_operation  = $v ->release_operation;
                    $head          = $v ->head;
                    $nickname      = $v ->nickname;
                    $city          = $v ->city;
                    $district      = $v ->district;
                    $content       = $v ->content;
                    $like          = $v ->like;
                    $position      = $v ->position;
                    $time = $v ->release_create_time;
                    $Nearby        = $this->Nearby($id);
                    $estimate      = $this ->estimate($user_id,$id);
                    if($estimate == true){
                        $ex   = '1';
                    }else{
                        $ex = '0';
                    }
                    $das[] = array('release_id'=>$id,
                        'release_operation' =>$release_operation,
                        'release_image' =>$release_image,
                        'head' =>$head,'nickname' =>$nickname,
                        'address' =>$city.$district,'content' =>$content,'like' =>$like,'release_create_time'=>$time,
                        'position' => $position,'sum' =>$Nearby,'estimate'=>$ex
                    );
                }

            }
            if (empty($das)) {
                return json::json('failure', '由于网络或者不正当原因失败', json::value(2));

            } else {
                return json::json('success', '获取全部数据成功', $das);
            }
        }
    }







}
